package br.com.ravi.crm.core.taglib;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import br.com.ravi.utils.DateUtils;
import br.com.ravi.utils.DateUtils.WeekOfYear;
import br.com.ravi.utils.NumberUtils;

public class ScheduleTagLibrary extends TagSupport {
	private static final long serialVersionUID = -2867211166263129319L;
	private String year;
	private String month;
	private Integer nuYear;
	private Integer nuMonth;
	private Integer width;
	private Integer height;

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Integer getNuYear() {
		return nuYear;
	}

	public void setNuYear(Integer nuYear) {
		this.nuYear = nuYear;
	}

	public Integer getNuMonth() {
		return nuMonth;
	}

	public void setNuMonth(Integer nuMonth) {
		this.nuMonth = nuMonth;
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			List<WeekOfYear> weekOfYear = DateUtils.getWeekOfYear(this.nuMonth, this.nuYear);
			out.println(ScheduleHtmlRenderer.renderHTML(weekOfYear, getOptions()));
			HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
			response.setCharacterEncoding("UTF-8");
		} catch (IOException e) {
			throw new JspException(e);
		}
		return SKIP_BODY;
	}

	private Options getOptions() {
		return new Options(this.width, this.height);
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.nuYear = NumberUtils.parse(year).intValue();
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.nuMonth = NumberUtils.parse(month).intValue();
		this.month = month;
	}
	
	protected static class Options {
		Integer width;
		Integer height;
		
		public Options(Integer width, Integer height) {
			this.width = width;
			this.height = height;
		}
	}
}